package com.msg.xml;

import java.util.ArrayList;

public class TicketLevel_Manipulator extends Table_Manipulator {

	private ArrayList<String> ticketLevels;
	
	public TicketLevel_Manipulator(Property prop) {
		super(prop);
		ticketLevels = new ArrayList<String>();
	}
	
	/**
	 * read different ticket levels' name from the XML file
	 */
	@Override
	protected String getSpecificData() {
		getTicketLevels();
		StringBuilder result = new StringBuilder();
		for(int i = 0; i < ticketLevels.size(); i++) {
			result.append(ticketLevels.get(i));
			result.append(",");
		}
		return result.toString();
	}

	/**
	 * check if the XML file has some ticket levels' names that don't exist in the database
	 * if yes, add this ticket level into database
	 */
	@Override
	protected String update() {
		DBRequest request = new DBRequest();
		request.resultCol.add("Ticket_Level_Name");
		request.entries.add("Ticket_Level_Name");
		DBhandler = DBHandler.getInstance();
		for(int i = 0; i < ticketLevels.size(); i++) {
			request.data.add(ticketLevels.get(i));
			if(!DBhandler.hasData("Ticket_Level", request))
				DBhandler.insertData("Ticket_Level", request);
			request.data.remove(ticketLevels.get(i));
		}
		return null;
	}
	
	@Override
	protected void pushData(Object o) {
	}
	
	/**
	 * clean the list get from the XMLHandler start with the tag "Price Code Level"
	 * so that only the characters are left in the list (e.g. 'A', 'B', 'C', 'D'...)
	 */
	private void getTicketLevels() {
		XMLhandler.findNodeByTag("***       PRICE CODE TABLE");
		ArrayList<String> tmpList = XMLhandler.getDataListByTag("EVENT");
		for(int i = 0; i < tmpList.size(); i++) {
			if(tmpList.get(i).length() == 1) {
				String str = tmpList.get(i);
				if((str.compareTo("Z") <= 0) && (str.compareTo("A") >= 0))
					ticketLevels.add(str);
			}
		}
		prop.setTICKETLEVEL_NUM(ticketLevels.size());
	}
}
